package com.sky.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sky.domain.SmsMemberPrice;
import com.sky.domain.SmsSkuFullReduction;
import com.sky.domain.SmsSkuLadder;
import com.sky.dto.MemberPriceDTO;
import com.sky.dto.SkuReductionDTO;
import com.sky.service.SmsMemberPriceService;
import com.sky.service.SmsSkuFullReductionService;
import com.sky.mapper.SmsSkuFullReductionMapper;
import com.sky.service.SmsSkuLadderService;
import com.sky.utils.StringUtils;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
* @author Administrator
* @description 针对表【sms_sku_full_reduction(商品满减信息)】的数据库操作Service实现
* @createDate 2024-06-30 16:43:10
*/
@Service
public class SmsSkuFullReductionServiceImpl extends ServiceImpl<SmsSkuFullReductionMapper, SmsSkuFullReduction> implements SmsSkuFullReductionService{

    @Resource
    private SmsSkuLadderService smsSkuLadderService;
    @Resource
    private SmsMemberPriceService smsMemberPriceService;

    /**
     * 添加sku的优惠信息
     * @param skuReductionDTO
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean addSkuReduction(SkuReductionDTO skuReductionDTO) {
        // 1、保存sku优惠信息
        if (skuReductionDTO.getFullCount() > 0){
            SmsSkuLadder smsSkuLadder = new SmsSkuLadder();
            BeanUtils.copyProperties(skuReductionDTO, smsSkuLadder);
            smsSkuLadder.setAddOther(skuReductionDTO.getCountStatus());
            smsSkuLadderService.save(smsSkuLadder);
        }

        // 2、保存sku满减信息
        if (skuReductionDTO.getFullPrice().compareTo(BigDecimal.ZERO) > 0){
            SmsSkuFullReduction smsSkuFullReduction = new SmsSkuFullReduction();
            BeanUtils.copyProperties(skuReductionDTO, smsSkuFullReduction);
            smsSkuFullReduction.setAddOther(skuReductionDTO.getPriceStatus());
            this.save(smsSkuFullReduction);
        }

        // 3、保存sku会员价格
        List<MemberPriceDTO> priceDTOList = skuReductionDTO.getMemberPrice();
        if (StringUtils.isNotEmpty(priceDTOList)){
            Set<SmsMemberPrice> memberPriceSet = priceDTOList.stream()
                    .filter(priceDTO -> priceDTO.getPrice().compareTo(BigDecimal.ZERO) > 0)
                    .map(priceDTO -> {
                        SmsMemberPrice smsMemberPrice = new SmsMemberPrice();
                        smsMemberPrice.setId(null);
                        smsMemberPrice.setMemberLevelId(priceDTO.getId());
                        smsMemberPrice.setMemberLevelName(priceDTO.getName());
                        smsMemberPrice.setMemberPrice(priceDTO.getPrice());
                        smsMemberPrice.setAddOther(skuReductionDTO.getPriceStatus());
                        smsMemberPrice.setSkuId(skuReductionDTO.getSkuId());
                        return smsMemberPrice;
            }).collect(Collectors.toSet());
            smsMemberPriceService.saveBatch(memberPriceSet);
        }
        return true;
    }
}




